<?php

session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');

// Variabili
$list = "";

$user_id = $_SESSION['uid'];

// Recupera tutti i logs da un certo intervallo di tempo
if ($r = $db -> query("SELECT * FROM logs WHERE log_date > (NOW() - INTERVAL 10 MINUTE) AND log_target = '$user_id' ORDER BY log_date DESC LIMIT 10"))
{
	$list = '';
		
	while($row = $r -> fetch_assoc())
	{
			// Variabili
		$log_type = $row['log_type'];
		$log_target = $row['log_target'];
		$log_from = $row['user_id'];
		
		// Recupera il nome del personaggio target
		if ($r1 = $db->query("SELECT user_name FROM users WHERE user_id = '$log_target'"))
		{
		
			$row1 = $r1 -> fetch_assoc();
				$target_name = $row1['user_name'];
		}
		
		// Recupera il nome del personaggio from
		if ($r1 = $db->query("SELECT user_name FROM users WHERE user_id = '$log_from'"))
		{
			$row1 = $r1 -> fetch_assoc();
			
			$from_name = $row1['user_name'];
		}
		
		switch ($log_type)
		{
			case 1:
				$list .= '<div class="alert alert-default" style="padding: 0.5em; margin: 0.1em;"><small><strong>' . $row['log_size'] . '</strong> exp ricevuti da ' . $from_name . '</small></div>';
			break;
			case 3:
				$item_name = explode(";", $row['log_msg']); 
				$item_name = $item_name[0];
				$list .= '<div class="alert alert-warning" style="padding: 0.5em; margin: 0.1em;"><small>' . $row['log_size'] . ' <strong>' . $item_name . '</strong> ricevuti da ' . $from_name . '</small></div>';
			break;
		}
		
	}
	
}

// Lista delle tag speciali
$tags = array('temp_title' => $g_name, 'temp_list' => $list);

// Carica il template e formatta le tag speciali
$temp = new template('style/userlogs.template.html');
$temp -> replace($tags);

// Visualizza l'output
echo $temp -> show();

// Chiude la connessione al database
$db -> close();

?>